﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Mclib
{
	/// <summary>
	/// A set of quadratic models representing every pairwise combination of categories.
	/// </summary>
	public class QuadraticModelSet
	{
		/// <summary>
		/// The categorization data.
		/// </summary>
		public readonly CatData Data;
		/// <summary>
		/// The number of model pairs.
		/// </summary>
		public readonly int Npairs=0;
		/// <summary>
		/// The list of quadratic models.
		/// </summary>
		public readonly QuadraticModel[] Models;
		/// <summary>
		/// Initialize the set of quadratic models.
		/// </summary>
		/// <param name="Data"></param>
		public QuadraticModelSet(CatData Data)
		{
			this.Data = Data;
			this.Npairs = Data.X.Length*(Data.X.Length-1)/2;
			this.Models = new QuadraticModel[this.Npairs];
			int i,j,ct=0;
			for (i=0; i<Data.X.Length-1; i++)
				for (j=i+1; j<Data.X.Length; j++)
				{
					//	Initialize a new quadratic model.
					Models[ct] = new QuadraticModel(this,i,j);
					//	Increment model counter
					ct++;
				}
		}
	}
	/// <summary>
	/// A quadratic model representing a unique pair of categories.
	/// </summary>
	public class QuadraticModel
	{
		/// <summary>
		/// All quadratic models must exist inside a set and are owned by that set.
		/// </summary>
		public readonly QuadraticModelSet Owner;
		/// <summary>
		/// The 0-based index of category "A" into Owner.CatData.X[_]
		/// </summary>
		public readonly int A;
		/// <summary>
		/// The 0-based index of category "B" into Owner.CatData.X[_]
		/// </summary>
		public readonly int B;
		/// <summary>
		/// A model for discriminating between two categories.
		/// </summary>
		/// <param name="Owner">The set of discriminant pairs.</param>
		/// <param name="A">The 0-based index of category "A"</param>
		/// <param name="B">The 0-based index of category "B"</param>
		public QuadraticModel(QuadraticModelSet Owner,int A,int B)
		{
			this.Owner = Owner;
			this.A = A;
			this.B = B;
		}
	}
}